#!/bin/bash
P_ADDRESS=localhost
P_HOSTNAME=localhost
P_PORT=8003
P_REMOTE_PATH="~$USER/common/"
P_IMPL_CODEBASE_PATH=file://$HOME/PAR/parcman/src/
P_POLICY="policy"
P_POLICY_GROUP="setup/policy"
P_DATABASE_DIR="$HOME/PAR/parcman/dbDirectory"

E_MC_INLCUDE_ARGSLIB=65
E_MC_INLCUDE_ARGSLIB_MSG="Impossibile includere argslib.sh"
E_MC_INLCUDE_LOGLIB=66
E_MC_INLCUDE_LOGLIB_MSG="Impossibile includere loglib.sh"

# include argslib.sh
. ../bash/argslib.sh 2> /dev/null
[[ $? -gt 0 ]] && echo $E_MC_INLCUDE_ARGSLIB_MSG && exit $E_MC_INLCUDE_ARGSLIB

# include loglib.sh
. ../bash/loglib.sh 2> /dev/null
[[ $? -gt 0 ]] && echo $E_MC_INLCUDE_LOGLIB_MSG && exit $E_MC_INLCUDE_LOGLIB

OPTIONS=( --address --hostname --port --remote-path --policy-file --policy-group --database-dir --impl-codebase-path --help )
HELPS=( "codebase address" "hostname" "codebase port" "codebase path" "policy file path" "policy group" "database directory" "codebase implementation path" "prints help page" )
DEFAULTS=( $P_ADDRESS $P_HOSTNAME $P_PORT $P_REMOTE_PATH $P_POLICY $P_POLICY_GROUP $P_DATABASE_DIR $P_IMPL_CODEBASE_PATH "")

G_LOGPOS=$(basename $0)
# catturo CTRL-C per terminare senza dare errori
trap 'rmid -stop; bt_info_inline "Disconnessione server in corso... " $G_LOGPOS' TERM INT

for i in $@; do
	case $i in 
	--address=*)
		argslib_get_long_option $i
		shift
		P_ADDRESS="$ARGSLIB_OPT_VALUE"
		;;
	--hostname=*)
		argslib_get_long_option $i
		shift
		P_HOSTNAME="$ARGSLIB_OPT_VALUE"
		;;
	--port=*)
		argslib_get_long_option $i
		shift
		P_PORT="$ARGSLIB_OPT_VALUE"
		;;
	--remote-path=*)
		argslib_get_long_option $i
		shift
		P_REMOTE_PATH="$ARGSLIB_OPT_VALUE"
		;;
	--policy-file=*)
		argslib_get_long_option $i
		shift
		P_POLICY="$ARGSLIB_OPT_VALUE"
		;;
	--policy-group=*)
		argslib_get_long_option $i
		shift
		P_POLICY_GROUP="$ARGSLIB_OPT_VALUE"
		;;
	--database-dir=*)
		argslib_get_long_option $i
		shift
		P_DATABASE_DIR="$ARGSLIB_OPT_VALUE"
		;;
	--impl-codebase-path=*)
		argslib_get_long_option $i
		shift
		P_IMPL_CODEBASE_PATH="$ARGSLIB_OPT_VALUE"
		;;
	--help)
		shift
		argslib_usage
		exit 0
		;;
	*)
		shift
		;;
	esac
done

bt_info_inline "parametri java:" $G_LOGPOS
bt_info_inline " -classpath :." $G_LOGPOS
bt_info_inline " -Djava.rmi.server.codebase=http://$P_ADDRESS:$P_PORT/$P_REMOTE_PATH" $G_LOGPOS
bt_info_inline " -Djava.security.policy="$P_POLICY"" $G_LOGPOS
bt_info_inline " -Djava.rmi.server.hostname=$P_HOSTNAME" $G_LOGPOS
bt_info_inline " -Dsetup.implCodebase="$P_IMPL_CODEBASE_PATH"" $G_LOGPOS
bt_info_inline " -Dsetup.loginServerClass=loginserver.LoginServer" $G_LOGPOS
bt_info_inline " -Dsetup.indexingServerClass=indexingserver.IndexingServer" $G_LOGPOS
bt_info_inline " -Dsetup.policyGroup="$P_POLICY_GROUP"" $G_LOGPOS
bt_info_inline " -Dsetup.dbDirectory="$P_DATABASE_DIR"" $G_LOGPOS
bt_info_inline " setup.Setup" $G_LOGPOS

bt_info_nl "java -classpath :. \
-Djava.rmi.server.codebase=http://$P_ADDRESS:$P_PORT/$P_REMOTE_PATH \
-Djava.security.policy="$P_POLICY" \
-Djava.rmi.server.hostname=$P_HOSTNAME \
-Dsetup.implCodebase="$P_IMPL_CODEBASE_PATH" \
-Dsetup.loginServerClass=loginserver.LoginServer \
-Dsetup.indexingServerClass=indexingserver.IndexingServer \
-Dsetup.policyGroup="$P_POLICY_GROUP" \
-Dsetup.dbDirectory="$P_DATABASE_DIR" \
setup.Setup" "start parcman server" "server-start" $BT_OUT_TYPE_NORMAL $BT_TYPE_EQ_TO 130

